package tako.wisers.com.android.apis.parser;

import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import tako.wisers.com.android.apis.database.SQListManager;
import tako.wisers.com.android.apis.parser.xml.item.Draw;
import tako.wisers.com.android.apis.util.Global;

public class ConfXMLParser extends XMLParserBase {
	public final static String VERSION_TAG = "version";
	public final static String DRAW_TAG = "draw";
	public final static String TEXT = "#text";
	public final static String NAME_ATTR = "name";
	public final static String VIEW_ID_ATTR = "viewID";
	public final static String LAYOUT_ID_ATTR = "layoutID";
	public final static String URL_ATTR = "url";
	public final static String DRAW_TABLE_NAME = "DRAW";

	private static ConfXMLParser mcxp;
	private String mVersion;
	private boolean readFromDB;
	private SQListManager mSQLManager;
	private Map<String, Map<String, Draw>> drawMap = new Hashtable<String, Map<String, Draw>>();

	private ConfXMLParser(boolean readFromDB, InputStream is) {
		try {
			mSQLManager = SQListManager.getInstance();
			this.readFromDB = readFromDB;
			build(is);
			if(!readFromDB){
				Iterator<Map<String, Draw>> itr = drawMap.values().iterator();
				Map<String, Draw> tempDrawMap = new HashMap<String, Draw>();
				while(itr.hasNext()){
					Map<String, Draw> drawList = itr.next();
					tempDrawMap.putAll(drawList);
				}
				mSQLManager.insert(new ArrayList<Draw>(tempDrawMap.values()), SQListManager.TABLE_INDATABASE);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public String getMVersion() {
		return mVersion;
	}


		public static void create(boolean readFromDB, InputStream is) {
		if (mcxp == null) {
			mcxp = new ConfXMLParser(readFromDB, is);
		}
	}
	
	public static ConfXMLParser getInstance(){
		return mcxp;
	}

	@Override
	protected void tagNameSwitch(Node node) {
		String name = node.getNodeName();
		if (VERSION_TAG.equalsIgnoreCase(name))
			versionTag(node);
		else if (DRAW_TAG.equalsIgnoreCase(name))
			drawTag(node);

		parser(node.getChildNodes());
	}

	private void drawTag(Node node) {
		if (readFromDB)
			return;
		
		String name = getNodeAttribute(node, NAME_ATTR);
		String url = getNodeAttribute(node, URL_ATTR);
		String viewIdStr = getNodeAttribute(node, VIEW_ID_ATTR);
		String layoutIdStr = getNodeAttribute(node, LAYOUT_ID_ATTR);
		
		Draw draw = new Draw(name, viewIdStr, layoutIdStr, url);
		addDraw(layoutIdStr, draw);
	}

	private void addDraw(String layoutName, Draw draw) {
		Map<String, Draw> tempMap = new HashMap<String, Draw>();
		if (drawMap.containsKey(layoutName)) {
			tempMap = drawMap.get(layoutName);
		}
		tempMap.put(draw.getName(), draw);
		drawMap.put(layoutName, tempMap);
	}

	private void versionTag(Node node) {
		String name = node.getNodeName();
		if (VERSION_TAG.equalsIgnoreCase(name)) {
			NodeList nodes = node.getChildNodes();
			for (int i = 0; i < nodes.getLength(); i++) {
				versionTag(nodes.item(i));
			}
		} else if (TEXT.equalsIgnoreCase(name)) {
			String version = node.getNodeValue();
			if(readFromDB){
				readDataFromDB();
			}else{
				mVersion = version;
				mSQLManager.clearTable(DRAW_TABLE_NAME, SQListManager.TABLE_INDATABASE);
			}
			return;
		}
	}

	private void readDataFromDB() {
		List<Draw> drawList = mSQLManager.select(Draw.class, SQListManager.TABLE_INDATABASE, null);
		Iterator<Draw> itr = drawList.iterator();
		while(itr.hasNext()){
			Draw draw = itr.next();
			addDraw(draw.getLayoutID(), draw);
		}
	}

	public Map<String, Draw> getDrawListByLayout(String layoutName) {
		return drawMap.get(layoutName);
	}

}
